<?php
    class URL
    {
        
        public static $actual_link = '';
        public static function getLink($sPath = '', $bSend = false, $sMessage = '')
        {
            if(strpos($sPath,"http") !== false)
            {
                $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
                preg_match($reg_exUrl, $sPath, $url);
                return $url[0];
            }

            $sLink = BASE_URL.$sPath;
            if($bSend)
            {
                if(!empty($sMessage))
                {
                    $_SESSION['message'] = $sMessage;
                }
                header('Location:'.$sLink);
                exit;
            }
            return $sLink;
        }

        public function getImageUrl($url, $suffix = '')
        {
            if(empty($suffix))
                return $url;
            $aPaths = pathinfo($url);
            return $aPaths['dirname'].'/'.$suffix.'/'.$aPaths['basename'];
        }

        public static function getShortLink($sController = '', $sAction = '', $sId = '')
        {
            $sLink = "~$sController/$sAction/$sId";
            return $sLink;
        }

        public static function getParam($sParam = '', $bString = false,$bFullCondition = false)
        {
            if(isset($_GET[$sParam]))
            {
                if(!$bString)
                {
                    if($_GET[$sParam] == '')
                    {
                        return false;
                    }
                }
                if($bFullCondition)
                {
                    return ($sParam.'='.$_GET[$sParam]);
                }
                else
                {
                    return $_GET[$sParam];
                }
            }
            else
            {
                if($bString)
                {
                    return '';
                }
                else
                {
                    return false;
                }
            }
        }

        public static function parse_url($url)
        {
            $encodedUrl = preg_replace('%[^:/?#&=\.]+%usDe', 'urlencode(\'$0\')', $url);
            $components = parse_url($encodedUrl);
            foreach ($components as &$component)
                $component = urldecode($component);
            return $components;
        }

        public static function convertLink($sLink)
        {
            $sLink = html_entity_decode($sLink);
            $info = parse_url($sLink);
            $safePath = implode('/', array_map('rawurlencode', explode('/', $info['path'])));
            $newUrl = sprintf('%s://%s%s', $info['scheme'], $info['host'], $safePath);
            return $newUrl;
        }

        public static function getInfo($sLink = '')
        {
            $oInfo = new stdClass();
            $aStrs = explode("/",$sLink);
            $bAdmin = false;
            $oInfo->sController = 'home';
            if(isset($aStrs[0]) && !empty($aStrs[0]))
            {
                if($aStrs[0] == 'admin')
                {
                    $bAdmin = true;
                    if(isset($aStrs[1]))
                    {
                        $oInfo->sController = $aStrs[1];
                    }
                }
                else
                {
                    $oInfo->sController = $aStrs[0];
                }
            }

            $oInfo->sAction = (isset($aStrs[1]) ? (!$bAdmin ? $aStrs[1] : (isset($aStrs[2]) ? $aStrs[2] : 'index')) : 'index') ;

            $oInfo->sId = (isset($aStrs[2]) ? (!$bAdmin ? $aStrs[2] : (isset($aStrs[3]) ? $aStrs[3] : '')) : '') ;
            $oInfo->sAlias =(isset($aStrs[3]) ? (!$bAdmin ? $aStrs[3] : (isset($aStrs[4]) ? $aStrs[4] : '')) : '') ;
            return $oInfo;
        }
        
        public static function getFullLink($sLink = '')
        {
            $oInfo = URL::getInfo($sLink);
            return trim($oInfo->sController.'/'.$oInfo->sAction.'/'.$oInfo->sId, '/');
        }
        
        public static function getCurrentLink()
        {
            if(!empty(URL::$actual_link))
            {
                return URL::$actual_link;
            }
            URL::$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
            return URL::$actual_link;
        }
    }

?>
